﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dreametryVersion2
{
    public partial class FrmCreateTask : Form
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        public FrmCreateTask()
        {
            InitializeComponent();
        }

        private void FrmCreateTask_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            frmMain form = new frmMain();
            form.btnRefresh.PerformClick();
            this.Close();
        }

        private void FrmCreateTask_Load_1(object sender, EventArgs e)
        {
            this.BackColor = ColorTranslator.FromHtml(global.themeColor);
            btnCreateTask.BackColor = ColorTranslator.FromHtml(global.themeColor);
            pnlTaskName.BackColor = ColorTranslator.FromHtml(global.themeColor);
            pnlReference.BackColor = ColorTranslator.FromHtml(global.themeColor);
            
            string commandText = "SELECT * FROM TaskCategory";

            using (SqlConnection connection = new SqlConnection(global.conString))
            using (SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection))
            {
                DataTable users = new DataTable();
                adapter.Fill(users);

                cmbCategory.DisplayMember = "category";
                cmbCategory.ValueMember = "category_id";
                cmbCategory.DataSource = users;
            }

            txtTaskName.Select();
        }

        private void btnCreateTask_Click(object sender, EventArgs e)
        {
            if(txtTaskName.Text == "" || txtReferenceLink.Text == "" || txtDescription.Text == "")
            {
                lblMessage.Text = "Please fill all the fields";
            }
            else
            {
                int count;
                String commandText = "SELECT COUNT(*) task_name FROM TaskTable WHERE task_name LIKE '" + txtTaskName.Text + "' and reported = 'no'";

                using (SqlConnection connection = new SqlConnection(global.conString))
                {
                    SqlCommand command = new SqlCommand(commandText, connection);
                    command.Connection.Open();
                    count = Int32.Parse(command.ExecuteScalar().ToString());
                }

                if(count > 0)
                {
                    lblMessage.Text = "Task has already been created";
                }
                else
                {
                    using (SqlConnection conn = new SqlConnection(global.conString))
                    { 
                        conn.Open();
                        SqlCommand cmd = new SqlCommand("CreateTask", conn);
                    
                        cmd.CommandType = CommandType.StoredProcedure;
                    
                        cmd.Parameters.Add(new SqlParameter("@taskname", txtTaskName.Text.Replace("'","").Replace("\"", "")));
                        cmd.Parameters.Add(new SqlParameter("@taskdescription", txtDescription.Text));
                        cmd.Parameters.Add(new SqlParameter("@refrenceLink", txtReferenceLink.Text));
                        cmd.Parameters.Add(new SqlParameter("@taskCategory", cmbCategory.SelectedValue));
                        cmd.Parameters.Add(new SqlParameter("@idEmp", global.employeeID));
                        //dont touch
                        cmd.Parameters.Add(new SqlParameter("@getdata", 1));
                        cmd.Parameters.Add(new SqlParameter("@getTaskid", 1));
                        //------------------
                        cmd.ExecuteNonQuery();
                    }
                    
                    
                    if (checkBoxWithMug.Checked == true)
                    {
                        using (SqlConnection conn = new SqlConnection(global.conString))
                        {
                            conn.Open();
                            SqlCommand cmd = new SqlCommand("CreateTask", conn);
                    
                            cmd.CommandType = CommandType.StoredProcedure;
                    
                            cmd.Parameters.Add(new SqlParameter("@taskname", txtTaskName.Text + " (Mug)"));
                            cmd.Parameters.Add(new SqlParameter("@taskdescription", txtDescription.Text));
                            cmd.Parameters.Add(new SqlParameter("@refrenceLink", txtReferenceLink.Text));
                            cmd.Parameters.Add(new SqlParameter("@taskCategory", 1));
                            cmd.Parameters.Add(new SqlParameter("@idEmp", global.employeeID));
                            //dont touch
                            cmd.Parameters.Add(new SqlParameter("@getdata", 1));
                            cmd.Parameters.Add(new SqlParameter("@getTaskid", 1));
                            //------------------
                            cmd.ExecuteNonQuery();
                        }
                    }
                    frmMain master = (frmMain)Application.OpenForms["frmMain"];
                    master.btnRefresh.PerformClick();

                    this.Hide();
                    String result = message.YesNo("Task creation", "Task has been created, would you like to create another?");
                    if(result == "Yes")
                    {
                        this.Show();
                        txtTaskName.Clear();
                        txtReferenceLink.Text = "N/A";
                        txtDescription.Clear();
                    }
                    else
                    {
                    this.Close();
                    }
                }
            }
        }

        private void cmbCategory_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (cmbCategory.Text == "Mug")
            {
                checkBoxWithMug.Visible = false;
                checkBoxWithMug.Checked = false;
            }
            else
            {
                checkBoxWithMug.Visible = true;
            }

            lblMessage.Text = "";
        }

        private void txtTaskName_TextChanged(object sender, EventArgs e)
        {
            lblMessage.Text = "";
        }

        private void txtReferenceLink_TextChanged(object sender, EventArgs e)
        {
            lblMessage.Text = "";
        }

        private void txtDescription_TextChanged(object sender, EventArgs e)
        {
            lblMessage.Text = "";
        }

        private void checkBoxWithMug_CheckedChanged(object sender, EventArgs e)
        {
            lblMessage.Text = "";
        }

        private void txtReferenceLink_Enter(object sender, EventArgs e)
        {
            if(txtReferenceLink.Text == "N/A")
            {
                txtReferenceLink.Text = "";
            }
        }

        private void txtReferenceLink_Leave(object sender, EventArgs e)
        {
            if (txtReferenceLink.Text == "")
            {
                txtReferenceLink.Text = "N/A";
            }
        }
    }
}
